package com.haoyeteng.sxbd.modular.business.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.haoyeteng.sxbd.core.constant.DeleteFlag;
import com.haoyeteng.sxbd.core.constant.SxbdConstants;
import com.haoyeteng.sxbd.core.exception.SxbdException;
import com.haoyeteng.sxbd.core.util.DateUtil;
import com.haoyeteng.sxbd.core.util.ToolUtil;
import com.haoyeteng.sxbd.modular.business.dao.OrderMapper;
import com.haoyeteng.sxbd.modular.business.model.Order;
import com.haoyeteng.sxbd.modular.business.model.OrderDtl;
import com.haoyeteng.sxbd.modular.business.service.IOrderDtlService;
import com.haoyeteng.sxbd.modular.business.service.IOrderService;
import com.haoyeteng.sxbd.modular.business.service.IServiceBuyingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 购买中心
 * @author chenz
 * </p>
 */
@Service
@Transactional
public class ServiceBuyingServiceImpl implements IServiceBuyingService {

	@Autowired
	private IOrderService orderService;


	/**
	 * 确认支付
	 * @param order 订单
	 * @return order
	 */
	@Override
	public Order payOrder(Order order){
		//校验
		Order orderAndDtl = orderService.selectOrderAndDtlById(order.getOrderId());
		if (ToolUtil.isEmpty(orderAndDtl)){
			throw new SxbdException("订单不存在！");
		}
		if (!SxbdConstants.ORDER_PAY_STATE_NOPAY.equals(orderAndDtl.getPayState())){
			throw new SxbdException("订单Id:" + order.getOrderId() + "不是待支付状态!");
		}
		//如果是银行待支付
		boolean ifBlankToPay = SxbdConstants.ORDER_PAY_STATE_NOPAY.equals(orderAndDtl.getPayState())
				&& ToolUtil.isNotEmpty(orderAndDtl.getSwiftNo())
				&& SxbdConstants.ORDER_PAY_TYPE_BANK_TRANSFER.equals(orderAndDtl.getPayType());
		if (ifBlankToPay){
			throw new SxbdException("订单Id:" + order.getOrderId() + "是银行转账待确认状态!");
		}
		if (ToolUtil.isEmpty(orderAndDtl.getDetails())){
			throw new SxbdException("获取订单Id:" + order.getOrderId() + "明细信息失败!");
		}
		orderAndDtl.setPayType(order.getPayType());
		 /*
        TODO 此处根据不同支付类型调用支付接口 回调函数更新订单状态
         */
		if(SxbdConstants.ORDER_PAY_TYPE_ALIPAY.equals(order.getPayType())){
			//支付宝支付
			//TODO 此处假设支付都是成功的 银行转账不修改状态  不生成账号
			/**
			 * 修改订单表
			 * 1.修改支付状态为已支付
			 * 2.生成差分账号
			 */
			boolean flag = orderService.updateAndSaveAccount(orderAndDtl);
			if(flag) {
				orderAndDtl.setPayState(SxbdConstants.ORDER_PAY_STATE_PAYMENTED);
			}
		}else if(SxbdConstants.ORDER_PAY_TYPE_WECHAT.equals(order.getPayType())){
			//微信支付
			//TODO 此处假设支付都是成功的 银行转账不修改状态  不生成账号
			/**
			 * 修改订单表
			 * 1.修改支付状态为已支付
			 * 2.生成差分账号
			 */
			boolean flag = orderService.updateAndSaveAccount(orderAndDtl);
			if(flag) {
				orderAndDtl.setPayState(SxbdConstants.ORDER_PAY_STATE_PAYMENTED);
			}
		}else if(SxbdConstants.ORDER_PAY_TYPE_BANK_TRANSFER.equals(order.getPayType())){
			//银行转账支付

			//设置支付方式
			Date date = new Date();
			Order orderVo = new Order();
			orderVo.setOrderId(order.getOrderId());
			if (ToolUtil.isNotEmpty(order.getPayType())){
				orderVo.setPayType(order.getPayType());
			}
			if (ToolUtil.isEmpty(order.getSwiftNo())) {
				throw new SxbdException("未获取到付款凭证号！请联系管理员");
			}
			orderVo.setSwiftNo(order.getSwiftNo());
			orderVo.setUpdateTime(date);
			orderService.updateById(orderVo);
		}else{
			throw new SxbdException("错误的付款方式！");
		}
		return orderAndDtl;
	}

}
